package net.itst.schwarzerdaumen;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class SchwarzerDaumen extends ListActivity {

    private static final int ACT_ADDPLANT = 1;
    private static final int ACT_WPLAN = 2;
    public SchwarzerDaumenDatabase database;
    private PlantsListAdapter adapPlants;
    private final Toast toast = null;
    private Cursor curPlants = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	database = new SchwarzerDaumenDatabase(this);

	startService(new Intent(SchwarzerDaumen.this, SDNotifier.class));
    }

    @Override
    public void onStart() {
	super.onStart();
	showPlantsList();
    }

    @Override
    public void onResume() {
	super.onResume();
	showPlantsList();
    }

    @Override
    public void onDestroy() {
	super.onDestroy();

	curPlants.close();
	database.close();
	stopService(new Intent(SchwarzerDaumen.this, SDNotifier.class));
    }

    public void showPlantsList() {
	curPlants = database.getReadableDatabase().query(
		TablePlants.TABLE_NAME, TablePlants.SQL_GET_STATUS, null, null,
		null, null, TablePlants.WNEXT);

	startManagingCursor(curPlants);
	adapPlants = new PlantsListAdapter(this, R.layout.plant_item,
		curPlants, TablePlants.FIELDS_GET_STATUS, new int[] {
			R.id.plant_name, R.id.plant_icon, R.id.plant_status });
	setListAdapter(adapPlants);

	View plants_list = this.getListView();
	registerForContextMenu(plants_list);
	plants_list.setOnCreateContextMenuListener(this);

    }

    private void addPlant() {
	Intent intent;
	intent = new Intent().setClass(this, AddPlant.class);

	startActivityForResult(intent, ACT_ADDPLANT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);
	/*
	 * switch (requestCode) { case ACT_ADDPLANT:
	 * toast.setText(R.string.t_plant_saved); break; case ACT_WPLAN:
	 * toast.setText(R.string.t_wplan_saved); break; default: Log.w("XXX",
	 * "Strange, got a callback from intent " + requestCode + " with result"
	 * + resultCode); } toast.setDuration(Toast.LENGTH_LONG); toast.show();
	 */
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View view,
	    ContextMenuInfo menuInfo) {
	super.onCreateContextMenu(menu, view, menuInfo);

	menu.setQwertyMode(true);
	menu.setHeaderTitle(R.string.cm_Title);
	menu.add(0, 0, 0, R.string.cm_Watering);
	menu.add(0, 1, 1, R.string.cm_Fertilizing);
	menu.add(0, 2, 2, R.string.cm_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
	super.onContextItemSelected(item);

	AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
		.getMenuInfo();

	Intent intent = new Intent();
	intent.putExtra("plant_id", menuInfo.id);

	switch (item.getItemId()) {
	case 0:
	    intent.setClass(getApplicationContext(), WPlan.class);
	    startActivityForResult(intent, ACT_WPLAN);
	}
	return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	super.onCreateOptionsMenu(menu);
	MenuInflater inflater = getMenuInflater();
	inflater.inflate(R.menu.plantmenu, menu);
	return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	super.onOptionsItemSelected(item);
	switch (item.getItemId()) {
	case R.id.addPlant:
	    addPlant();
	    return true;
	}
	return false;
    }

}